﻿using System;
using System.Data.Metadata.Edm;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Anata.WCFClient;
using Anata.WCFClient.AnataWCFService;
using System.Data;
using Anata.WCFClient.Properties;
using System.Windows.Threading;

namespace Anata.WCFClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        #region Data

        DateTime lastpush;
        DateTime lastpull;
        DispatcherTimer dispatchertimer;

        enum pull_table_opt { Agama, CabangLayanan, CabangProduk, FreeGift, GolonganDarah, JenisAset, JenisPembayaran, JenisStok, Karyawan, KaryawanLayanan, Konsumen, Layanan, MutasiStok, Role, Status }
        enum push_table_opt { KaryawanKehadiran, Konsumen, MutasiRekening, MutasiStok, Stok, Transaksi, TransaksiLayanan, TransaksiPembayaran, TransaksiProduk }

        enum SyncType { auto, push, pull }
        enum SyncDetail { def, man }
        enum Sync { ready, started }

        Sync sync; SyncDetail syncdetail; SyncType synctype;

        int idcabang;
        int row_id;

        double value;

        #endregion

        private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);

        public MainWindow()
        {
            InitializeComponent();

            grid2.Visibility = System.Windows.Visibility.Hidden;
            grid3.Visibility = System.Windows.Visibility.Hidden;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            radioButton3.IsChecked = true;
            radioButton4.IsChecked = true;

            progressBar1.Minimum = 0;
            value = 0;

            synctype = SyncType.auto;
            syncdetail = SyncDetail.def;
            sync = Sync.ready;

            button1.IsEnabled = true;
        }

        private void TimerTick(object sender, EventArgs e)
        {

            // Updating the Label which displays the current second
            label2.Content = "Next AutoSync in " + (60 - DateTime.Now.Minute) + " Minutes and " + (60 - DateTime.Now.Second) + " Seconds";

            // Forcing the CommandManager to raise the RequerySuggested event
            CommandManager.InvalidateRequerySuggested();
        }

        private void AutoSync()
        {
            AnataEntities context;
            AnataEntities realcontext = new AnataEntities();

            context = realcontext;
            value = 0;
            progressBar1.Maximum = short.MaxValue / 7;

            UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar1.SetValue);
            MessageBox.Show("Code #" + short.MinValue + "-" + short.MaxValue + " Enter Auto Mode.");

            while (value <= progressBar1.Maximum)
            {
                value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
            }

            context.SaveChanges(); context.Dispose();
        }

        public bool SyncDatabase()
        {
            #region Data

            Service1Client client = new Service1Client();

            List<Object> objek = new List<Object>();
            List<Object> list = new List<Object>();

            object[] objectarray;
            List<object[]> listofobjectarray = new List<object[]>();

            DataSet dataset = new DataSet();
            DataTable datatable = new DataTable();

            lastpush = Settings.Default.LastPushTimeStamp;
            lastpull = Settings.Default.LastPullTimeStamp;
            idcabang = Settings.Default.IdCabang;

            //Settings.Default.LastPullTimeStamp = DateTime.Now;
            //Settings.Default.LastPushTimeStamp = DateTime.Now;
            //Settings.Default.IdCabang = 1;
            //Settings.Default.Save();

            #endregion

            if (synctype == SyncType.auto)
            {
                try
                {
                    //TODO
                    dispatchertimer = new DispatcherTimer();
                    dispatchertimer.Tick += new EventHandler(TimerTick);
                    dispatchertimer.Interval = new TimeSpan(0, 0, 1);
                    dispatchertimer.Start();

                    AutoSync();
                    return true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            else if (synctype == SyncType.push)
            {
                try
                {
                    AnataEntities context;
                    AnataEntities realcontext = new AnataEntities();

                    context = realcontext;
                    value = 0;
                    progressBar1.Maximum = 15;

                    UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar1.SetValue);

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #region Push Services
                    /*
                    	Input:  
                        - dataset -> row data yang di push ke server
                    	Output: id (PK) yang dihasilkan
                    	Deskripsi: menambah satu row baru di tabel konsumen terpusat, dan mengembalikan id yang dihasilkan.
                       Asumsi: 
                        - client hanya menambah data baru, tidak melakukan update data.
                        - perubahan data hanya dilakukan di client.
                       Unsolved Problem: 
                        - client tidak dapat mengupdate id row yg dikirimkannya dengan id yang dihasilkan server
                        karena id tersebut merupakan key information dari instant tersebut.
                        - berbeda dengan pull service yang menyimpan data di client (satu intan context untuk seluruh tabel), 
                        push menyimpan data di server (satu context untuk tiap kiriman data) yang artinya kegagalan di client mungkin tetap menambah data baru di server.
                       Usulan Solusi: 
                        - tabel di server menambah field id untuk menyimpan id client yg diterima.
                        - untuk update edmx, jika ingin menambah field baru didatabase tambah di urutan akhir,
                        jangan ditengah, mempengaruhi susunan objectarray yang telah ada, malas cek lagi :)
                    */

                    #region Push KaryawanKehadiran

                    objectarray = new object[10];
                    list.Clear();

                    //revert true to false when done debugging
                    var karyawankehadiranquery = from row in context.KaryawanKehadiran where row.HasBeenPushed == true && row.LastUpdated > lastpush select row;

                    foreach (KaryawanKehadiran item in karyawankehadiranquery.ToList())
                    {
                        objectarray[0] = item.IdKaryawanKehadiran;
                        objectarray[1] = item.IdKaryawan;
                        objectarray[2] = item.WaktuMasuk;
                        objectarray[3] = item.WaktuKeluar;
                        objectarray[4] = item.IdUser;
                        objectarray[5] = item.LastUpdated;
                        item.HasBeenPushed = true;
                        objectarray[6] = item.HasBeenPushed;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[7] = item.HasBeenPushedTS;
                        objectarray[8] = item.IsIjin;
                        objectarray[9] = item.IsLembur;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushKaryawanKehadiran(dataset);

                        //KaryawanKehadiran temp = new KaryawanKehadiran();
                        //temp = item;
                        //temp.IdKaryawanKehadiran = row_id;

                        //AnataEntities temp = new AnataEntities();
                        //temp.KaryawanKehadiran.First(row => row.IdKaryawan == item.IdKaryawan &&
                        //    row.WaktuMasuk == item.WaktuMasuk &&
                        //    row.WaktuKeluar == item.WaktuKeluar &&
                        //    row.HasBeenPushedTS == item.HasBeenPushedTS
                        //).IdKaryawanKehadiran = row_id;

                        //temp.Dispose();

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push Konsumen

                    objectarray = new object[10];
                    list.Clear();

                    var konsumenquery = from row in context.Konsumen where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (Konsumen item in konsumenquery.ToList())
                    {
                        objectarray[0] = item.IdKonsumen;
                        objectarray[1] = item.Nama;
                        objectarray[2] = item.Alamat;
                        objectarray[3] = item.NoTelepon;
                        objectarray[4] = item.JenisKelamin;
                        objectarray[5] = item.TanggalLahir;
                        objectarray[6] = item.IdUser;
                        objectarray[7] = item.LastUpdated;
                        item.HasBeenPushed = true;
                        objectarray[8] = item.HasBeenPushed;
                        objectarray[9] = item.IdJenisKelamin;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushKonsumen(dataset);

                        //Konsumen temp = new Konsumen();
                        //temp = item;
                        //temp.IdKonsumen = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push MutasiRekening

                    objectarray = new object[13];
                    list.Clear();

                    var mutasirekeningquery = from row in context.MutasiRekening where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (MutasiRekening item in mutasirekeningquery.ToList())
                    {
                        objectarray[0] = item.IdMutasiRekening;
                        objectarray[1] = item.NominalMutasi;
                        objectarray[2] = item.IsOtomatis;
                        objectarray[3] = item.IdUser;
                        objectarray[4] = item.WaktuMutasi;
                        objectarray[5] = item.IdCabang;
                        objectarray[6] = item.LastUpdated;
                        item.HasBeenPushed = true;
                        objectarray[7] = item.HasBeenPushed;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[8] = item.HasBeenPushedTS;
                        objectarray[9] = item.IdRekening;
                        objectarray[10] = item.IdJenisMutasiRekening;
                        objectarray[11] = item.Keterangan;
                        objectarray[12] = item.SaldoRekening;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushMutasiRekening(dataset);

                        //MutasiRekening temp = new MutasiRekening();
                        //temp = item;
                        //temp.IdMutasiRekening = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push MutasiStok

                    objectarray = new object[10];
                    list.Clear();

                    var mutasistokquery = from row in context.MutasiStok where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (MutasiStok item in mutasistokquery.ToList())
                    {
                        objectarray[0] = item.IdMutasiStok;
                        objectarray[1] = item.JumlahMutasi;
                        objectarray[2] = item.Keterangan;
                        objectarray[3] = item.WaktuMutasi;
                        objectarray[4] = item.IdStok;
                        objectarray[5] = item.IdUser;
                        objectarray[6] = item.LastUpdated;
                        item.HasBeenPushed = true;
                        objectarray[7] = item.HasBeenPushed;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[8] = item.HasBeenPushedTS;
                        objectarray[9] = item.IdJenisMutasiStok;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushMutasiStok(dataset);

                        //MutasiStok temp = new MutasiStok();
                        //temp = item;
                        //temp.IdMutasiStok = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push Stok 
                    
                    objectarray = new object[14];
                    list.Clear();

                    var stokquery = from row in context.Stok where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;
                                        
                    foreach (Stok item in stokquery.ToList())
                    {
                        objectarray[0] = item.IdStok;
                        objectarray[1] = item.IdJenisStok;
                        objectarray[2] = item.IdCabang;
                        objectarray[3] = item.JumlahBeli;
                        objectarray[4] = item.WaktuBeli;
                        objectarray[5] = item.TanggalKadaluarsa;
                        objectarray[6] = item.Catatan;
                        objectarray[7] = item.IdUser;
                        objectarray[8] = item.WaktuUpdate;
                        objectarray[9] = item.LastUpdated;
                        item.HasBeenPushed = true;
                        objectarray[10] = item.HasBeenPushed;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[11] = item.HasBeenPushedTS;
                        objectarray[12] = item.HargaBeli;
                        objectarray[13] = item.SumJumlahMutasiStok;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushStok(dataset);

                        //Stok temp = new Stok();
                        //temp = item;
                        //temp.IdStok = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                    
                    #endregion

                    #region Push Transaksi

                    objectarray = new object[14];
                    list.Clear();

                    var transaksiquery = from row in context.Transaksi where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (Transaksi item in transaksiquery.ToList())
                    {
                        objectarray[00] = item.IdTransaksi;
                        objectarray[01] = item.WaktuBuat;
                        objectarray[02] = item.WaktuSelesai;
                        objectarray[03] = item.TotalHarga;
                        objectarray[04] = item.PotonganHarga;
                        objectarray[05] = item.KoreksiHarga;
                        objectarray[06] = item.IdKonsumen;
                        objectarray[07] = item.IdCabang;
                        objectarray[08] = item.IdUser;
                        objectarray[09] = item.LastUpdated;
                        objectarray[10] = item.IsBatal;
                        item.HasBeenPushed = true;
                        objectarray[11] = item.HasBeenPushed;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[12] = item.HasBeenPushedTS;
                        objectarray[13] = item.WaktuBatal;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushTransaksi(dataset);

                        //Transaksi temp = new Transaksi();
                        //temp = item;
                        //temp.IdTransaksi = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push TransaksiLayanan

                    objectarray = new object[23];
                    list.Clear();

                    var tlayananquery = from row in context.TransaksiLayanan where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (TransaksiLayanan item in tlayananquery.ToList())
                    {
                        objectarray[00] = item.IdTransaksiLayanan;
                        objectarray[01] = item.IdTransaksi;
                        objectarray[02] = item.IdLayanan;
                        objectarray[03] = item.IdKaryawan;
                        objectarray[04] = item.HargaLayanan;
                        objectarray[05] = item.PotonganPromo;
                        objectarray[06] = item.WaktuBuat;
                        objectarray[07] = item.WaktuTunggu;
                        objectarray[08] = item.WaktuMulai;
                        objectarray[09] = item.WaktuSelesai;
                        objectarray[10] = item.IdUser;
                        objectarray[11] = item.IdJenisStok;
                        objectarray[12] = item.NamaPromo;
                        objectarray[13] = item.NamaFreeGift;
                        objectarray[14] = item.JumlahFreeGift;
                        objectarray[15] = item.LastUpdated;
                        objectarray[16] = item.NilaiKepuasan;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[17] = item.HasBeenPushedTS;
                        objectarray[18] = item.IsBatal;
                        item.HasBeenPushed = true;
                        objectarray[19] = item.HasBeenPushed;
                        objectarray[20] = item.WaktuBatal;
                        objectarray[21] = item.IsLangganan;
                        objectarray[22] = item.KomisiKaryawan;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushTransaksiLayanan(dataset);

                        //TransaksiLayanan temp = new TransaksiLayanan();
                        //temp = item;
                        //temp.IdTransaksiLayanan = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push TransaksiPembayaran

                    objectarray = new object[9];
                    list.Clear();

                    var tpembayaranquery = from row in context.TransaksiPembayaran where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (TransaksiPembayaran item in tpembayaranquery.ToList())
                    {
                        objectarray[0] = item.IdTransaksiPembayaran;
                        objectarray[1] = item.IdTransaksi;
                        objectarray[2] = item.IdJenisPembayaran;
                        objectarray[3] = item.NominalPembayaran;
                        objectarray[4] = item.IdUser;
                        objectarray[5] = item.LastUpdated;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[6] = item.HasBeenPushedTS;
                        item.HasBeenPushed = true;
                        objectarray[7] = item.HasBeenPushed;
                        objectarray[8] = item.IdRekening;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushTransaksiPembayaran(dataset);

                        //TransaksiPembayaran temp = new TransaksiPembayaran();
                        //temp = item;
                        //temp.IdTransaksiPembayaran = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Push TransaksiProduk

                    objectarray = new object[14];
                    list.Clear();

                    var tprodukquery = from row in context.TransaksiProduk where row.HasBeenPushed == false && row.LastUpdated > lastpush select row;

                    foreach (TransaksiProduk item in tprodukquery.ToList())
                    {
                        objectarray[00] = item.IdTransaksiProduk;
                        objectarray[01] = item.IdTransaksi;
                        objectarray[02] = item.IdJenisStok;
                        objectarray[03] = item.IdKaryawan;
                        objectarray[04] = item.JumlahProduk;
                        objectarray[05] = item.HargaProduk;
                        objectarray[06] = item.PotonganHarga;
                        objectarray[07] = item.IdUser;
                        objectarray[08] = item.LastUpdated;
                        item.HasBeenPushedTS = lastpush;
                        objectarray[09] = lastpush;
                        item.HasBeenPushed = true;
                        objectarray[10] = item.HasBeenPushed;
                        objectarray[11] = item.IsBatal;
                        objectarray[12] = item.WaktuBatal;
                        objectarray[13] = item.KomisiKaryawan;

                        dataset = new DataSet();
                        datatable = new DataTable();

                        list.Add((object[])objectarray.Clone());
                        for (int i = 0; i < objectarray.Length; ++i) { datatable.Columns.Add(); }
                        foreach (object[] a in list) { datatable.Rows.Add(a); }

                        dataset.Tables.Add(datatable);
                        row_id = client.PushTransaksiProduk(dataset);

                        //TransaksiProduk temp = new TransaksiProduk();
                        //temp = item;
                        //temp.IdTransaksiProduk = row_id;

                        list.Clear();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion
                    
                    #endregion

                    value = progressBar1.Maximum; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                    context.SaveChanges(); context.Dispose();

                    return true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            else if (synctype == SyncType.pull)
            {
                try
                {
                    AnataEntities context;
                    AnataEntities realcontext = new AnataEntities();

                    context = realcontext;
                    value = 0;
                    progressBar1.Maximum = 15;

                    UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(progressBar1.SetValue);

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #region Pull Service
                    /*
                    	Input:  
                        - lastPull -> waktu terakhir kali client melakukan penarikan data, 
                        - idcabang -> integer PK cabang
                    	Output: struktur data yang bersesuaian dengan tabel yang diminta
                    	Deskripsi: mengquery database terpusat dan mengambil data yang ‘lastUpdated’ nya lebih baru daripada ‘lastPull’.
                       Asumsi: 
                        - data yang dikirim adalah data yang lastupdate/createnya lebih dari timestamp pull terakhir pada masing2 client
                        - client sama sekali tidak melakukan perubahan terhadap isi [TABEL] yg di pull.
                        - perubahan data hanya dilakukan di server.
                       Unsolved Problem: ketika data disimpan ke tabel client, ada kemungkinan id di client berbeda dengan di server
                        karena masih terdapat trigger untuk id di client, hal ini bisa mengakibatkan duplikasi data.
                       Usulan Solusi: tabel client yang datanya di push dari server idny tidak ditrigger secara otomatis.
                    */

                    #region Pull Agama

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullAgama(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Agama editedrow = new Agama();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Agama where row.IdAgama == id select row;

                        if (query.ToList().Count == 0) context.Agama.AddObject(editedrow);
                        else editedrow = context.Agama.First(row => row.IdAgama == id);

                        editedrow.IdAgama = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.Kode = item[4].ToString();
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull CabangLayanan

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullCabangLayanan(lastpull, idcabang).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        CabangLayanan editedrow = new CabangLayanan();
                        int id = Int32.Parse(item[0].ToString());
                        int id2 = Int32.Parse(item[1].ToString());
                        var query = from row in context.CabangLayanan where row.IdCabang == id && row.IdLayanan == id2 select row;

                        if (query.ToList().Count == 0) context.CabangLayanan.AddObject(editedrow);
                        else editedrow = context.CabangLayanan.First(row => row.IdCabang == id && row.IdLayanan == id2);

                        editedrow.IdCabang = id;
                        editedrow.IdLayanan = id2;
                        if (item[2] != System.DBNull.Value) editedrow.HargaLayanan = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[4].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull CabangProduk

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullCabangProduk(lastpull, idcabang).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        CabangProduk editedrow = new CabangProduk();
                        int id = Int32.Parse(item[0].ToString());
                        int id2 = Int32.Parse(item[1].ToString());
                        var query = from row in context.CabangProduk where row.IdCabang == id && row.IdJenisStok == id2 select row;

                        if (query.ToList().Count == 0) context.CabangProduk.AddObject(editedrow);
                        else editedrow = context.CabangProduk.First(row => row.IdCabang == id && row.IdJenisStok == id2);

                        editedrow.IdCabang = id;
                        editedrow.IdJenisStok = id2;
                        if (item[2] != System.DBNull.Value) editedrow.HargaProduk = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[4].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull FreeGift

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullFreeGift(lastpull, idcabang).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        FreeGift editedrow = new FreeGift();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.FreeGift where row.IdFreeGift == id select row;

                        if (query.ToList().Count == 0) context.FreeGift.AddObject(editedrow);
                        else editedrow = context.FreeGift.First(row => row.IdFreeGift == id);

                        editedrow.IdFreeGift = id;
                        if (item[1] != System.DBNull.Value) editedrow.IdLayanan = Int32.Parse(item[2].ToString());
                        if (item[2] != System.DBNull.Value) editedrow.IdCabang = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[4] != System.DBNull.Value) editedrow.IdJenisStok = Int32.Parse(item[2].ToString());
                        if (item[5] != System.DBNull.Value) editedrow.Jumlah = Int32.Parse(item[5].ToString());
                        if (item[6] != System.DBNull.Value) editedrow.IsAktif = (item[6].ToString() == "true") ? true : false;
                        if (item[7] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[8] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[8].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull GolonganDarah

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullGolonganDarah(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        GolonganDarah editedrow = new GolonganDarah();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.GolonganDarah where row.IdGolonganDarah == id select row;

                        if (query.ToList().Count == 0) context.GolonganDarah.AddObject(editedrow);
                        else editedrow = context.GolonganDarah.First(row => row.IdGolonganDarah == id);

                        editedrow.IdGolonganDarah = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull JenisAset

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullJenisAset(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        JenisAset editedrow = new JenisAset();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.JenisAset where row.IdJenisAset == id select row;

                        if (query.ToList().Count == 0) context.JenisAset.AddObject(editedrow);
                        else editedrow = context.JenisAset.First(row => row.IdJenisAset == id);

                        editedrow.IdJenisAset = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull JenisPembayaran

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullJenisPembayaran(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        JenisPembayaran editedrow = new JenisPembayaran();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.JenisPembayaran where row.IdJenisPembayaran == id select row;

                        if (query.ToList().Count == 0) context.JenisPembayaran.AddObject(editedrow);
                        else editedrow = context.JenisPembayaran.First(row => row.IdJenisPembayaran == id);

                        editedrow.IdJenisPembayaran = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IsTunai = (item[2].ToString() == "true") ? true : false;
                        if (item[3] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[4].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull JenisStok

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullJenisStok(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        JenisStok editedrow = new JenisStok();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.JenisStok where row.IdJenisStok == id select row;

                        if (query.ToList().Count == 0) context.JenisStok.AddObject(editedrow);
                        else editedrow = context.JenisStok.First(row => row.IdJenisStok == id);

                        editedrow.IdJenisStok = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Karyawan

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullKaryawan(lastpull, idcabang).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Karyawan editedrow = new Karyawan();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Karyawan where row.IdKaryawan == id select row;

                        if (query.ToList().Count == 0) context.Karyawan.AddObject(editedrow);
                        else editedrow = context.Karyawan.First(row => row.IdKaryawan == id);

                        editedrow.IdKaryawan = id;
                        if (item[01] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[02] != System.DBNull.Value) editedrow.Alamat = item[2].ToString();
                        if (item[03] != System.DBNull.Value) editedrow.NoTelepon = item[3].ToString();
                        if (item[04] != System.DBNull.Value) editedrow.NoKTP = item[4].ToString();
                        if (item[05] != System.DBNull.Value) editedrow.IdJenisKelamin = Int32.Parse(item[5].ToString());
                        if (item[06] != System.DBNull.Value) editedrow.TanggalLahir = DateTime.Parse(item[6].ToString());
                        if (item[07] != System.DBNull.Value) editedrow.IdStatus = Int32.Parse(item[7].ToString());
                        if (item[08] != System.DBNull.Value) editedrow.IdAgama = Int32.Parse(item[8].ToString());
                        if (item[09] != System.DBNull.Value) editedrow.IdGolonganDarah = Int32.Parse(item[9].ToString());
                        if (item[10] != System.DBNull.Value) editedrow.TanggalMasuk = DateTime.Parse(item[10].ToString());
                        if (item[11] != System.DBNull.Value) editedrow.TanggalKeluar = DateTime.Parse(item[11].ToString());
                        if (item[12] != System.DBNull.Value) editedrow.IdCabang = Int32.Parse(item[12].ToString());
                        if (item[13] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[13].ToString());
                        if (item[14] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[14].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull KaryawanLayanan

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullKaryawanLayanan(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        KaryawanLayanan editedrow = new KaryawanLayanan();
                        int id = Int32.Parse(item[0].ToString());
                        int id2 = Int32.Parse(item[1].ToString());
                        var query = from row in context.KaryawanLayanan where row.IdKaryawan == id && row.IdLayanan == id2 select row;

                        if (query.ToList().Count == 0) context.KaryawanLayanan.AddObject(editedrow);
                        else editedrow = context.KaryawanLayanan.First(row => row.IdKaryawan == id && row.IdLayanan == id2);

                        editedrow.IdKaryawan = id;
                        editedrow.IdLayanan = id2;
                        if (item[2] != System.DBNull.Value) editedrow.Komisi = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[4].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Konsumen

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullKonsumen(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Konsumen editedrow = new Konsumen();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Konsumen where row.IdKonsumen == id select row;

                        if (query.ToList().Count == 0) context.Konsumen.AddObject(editedrow);
                        else editedrow = context.Konsumen.First(row => row.IdKonsumen == id);

                        editedrow.IdKonsumen = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.Alamat = item[2].ToString();
                        if (item[3] != System.DBNull.Value) editedrow.NoTelepon = item[3].ToString();
                        if (item[4] != System.DBNull.Value) editedrow.IdJenisKelamin = Int32.Parse(item[4].ToString());
                        if (item[5] != System.DBNull.Value) editedrow.TanggalLahir = DateTime.Parse(item[5].ToString());
                        if (item[6] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[6].ToString());
                        if (item[7] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[7].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Layanan

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullLayanan(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Layanan editedrow = new Layanan();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Layanan where row.IdLayanan == id select row;

                        if (query.ToList().Count == 0) context.Layanan.AddObject(editedrow);
                        else editedrow = context.Layanan.First(row => row.IdLayanan == id);

                        editedrow.IdLayanan = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.Durasi = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[3].ToString());
                        if (item[4] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[4].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Promo

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullPromo(lastpull, idcabang).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Promo editedrow = new Promo();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Promo where row.IdPromo == id select row;

                        if (query.ToList().Count == 0) context.Promo.AddObject(editedrow);
                        else editedrow = context.Promo.First(row => row.IdPromo == id);

                        editedrow.IdLayanan = id;
                        if (item[01] != System.DBNull.Value) editedrow.IdLayanan = Int32.Parse(item[1].ToString());
                        if (item[02] != System.DBNull.Value) editedrow.IdCabang = Int32.Parse(item[2].ToString());
                        if (item[03] != System.DBNull.Value) editedrow.Nama = item[3].ToString();
                        if (item[04] != System.DBNull.Value) editedrow.IsPersentase = (item[4].ToString() == "true") ? true : false;
                        if (item[05] != System.DBNull.Value) editedrow.PersentasePromo = Int32.Parse(item[5].ToString());
                        if (item[06] != System.DBNull.Value) editedrow.NominalPromo = Int32.Parse(item[6].ToString());
                        if (item[07] != System.DBNull.Value) editedrow.Syarat = item[7].ToString();
                        if (item[08] != System.DBNull.Value) editedrow.IsOnce = (item[8].ToString() == "true") ? true : false;
                        if (item[09] != System.DBNull.Value) editedrow.WaktuMulai = DateTime.Parse(item[9].ToString());
                        if (item[10] != System.DBNull.Value) editedrow.WaktuSelesai = DateTime.Parse(item[10].ToString());
                        if (item[11] != System.DBNull.Value) editedrow.IsDaily = (item[11].ToString() == "true") ? true : false;
                        if (item[12] != System.DBNull.Value) editedrow.IsWeekly = (item[12].ToString() == "true") ? true : false;
                        if (item[13] != System.DBNull.Value) editedrow.IsMonthly = (item[13].ToString() == "true") ? true : false;
                        if (item[14] != System.DBNull.Value) editedrow.IsYearly = (item[14].ToString() == "true") ? true : false;
                        if (item[15] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[15].ToString());
                        if (item[16] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[16].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Role

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullRole(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Role editedrow = new Role();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Role where row.IdRole == id select row;

                        if (query.ToList().Count == 0) context.Role.AddObject(editedrow);
                        else editedrow = context.Role.First(row => row.IdRole == id);

                        editedrow.IdRole = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                    }

                    value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });

                    #endregion

                    #region Pull Status

                    listofobjectarray.Clear();

                    foreach (DataRow dr in client.PullStatus(lastpull).Tables[0].Rows)
                    {
                        objectarray = dr.ItemArray;
                        listofobjectarray.Add(objectarray);
                    }

                    foreach (object[] item in listofobjectarray)
                    {
                        Status editedrow = new Status();
                        int id = Int32.Parse(item[0].ToString());
                        var query = from row in context.Status where row.IdStatus == id select row;

                        if (query.ToList().Count == 0) context.Status.AddObject(editedrow);
                        else editedrow = context.Status.First(row => row.IdStatus == id);

                        editedrow.IdStatus = id;
                        if (item[1] != System.DBNull.Value) editedrow.Nama = item[1].ToString();
                        if (item[2] != System.DBNull.Value) editedrow.IdUser = Int32.Parse(item[2].ToString());
                        if (item[3] != System.DBNull.Value) editedrow.LastUpdated = DateTime.Parse(item[3].ToString());
                    }

                    #endregion

                    #endregion

                    value = progressBar1.Maximum; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value });
                    context.SaveChanges(); context.Dispose();

                    return true;
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }

            return false;
        }

        #region Visibility

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            progressBar1.Value = 0;

            if (sync == Sync.ready)
            {                
                grid1.Visibility = System.Windows.Visibility.Hidden;
                grid2.Visibility = System.Windows.Visibility.Hidden;
                grid3.Visibility = System.Windows.Visibility.Hidden;
                grid4.Visibility = System.Windows.Visibility.Visible;
                button1.Content = "Stop";
                sync = Sync.started;

                if (synctype != SyncType.auto)
                    label2.Content = "ON";

                if (SyncDatabase())
                {
                    if (synctype != SyncType.auto)
                    {
                        label2.Content = "OFF";
                        radioButton3.IsChecked = true;
                        button1.Content = "Start";
                        sync = Sync.ready;
                        MessageBox.Show("Sync Finished");
                    }
                }
                else
                {
                    if (synctype != SyncType.auto)
                    {
                        label2.Content = "OFF";
                        radioButton3.IsChecked = true;
                        button1.Content = "Start";
                        sync = Sync.ready;
                        MessageBox.Show("Sync Failed");
                    }
                }

                grid1.Visibility = System.Windows.Visibility.Visible;
                grid2.Visibility = System.Windows.Visibility.Hidden;
                grid3.Visibility = System.Windows.Visibility.Hidden;
                grid4.Visibility = System.Windows.Visibility.Hidden;
            }
            else if (sync == Sync.started)
            {
                if (synctype == SyncType.auto) dispatchertimer.Stop();

                grid1.Visibility = System.Windows.Visibility.Visible;
                grid2.Visibility = System.Windows.Visibility.Hidden;
                grid3.Visibility = System.Windows.Visibility.Hidden;
                grid4.Visibility = System.Windows.Visibility.Hidden;
                radioButton3.IsChecked = true;
                label2.Content = "OFF";
                button1.Content = "Start";
                sync = Sync.ready;
            }
        }

        private void radioButton3_Checked(object sender, RoutedEventArgs e)
        {
            synctype = SyncType.auto;

            grid2.Visibility = System.Windows.Visibility.Hidden;
            grid3.Visibility = System.Windows.Visibility.Hidden;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            progressBar1.Value = 0;
            button1.IsEnabled = true;
        }

        private void radioButton2_Checked(object sender, RoutedEventArgs e)
        {
            synctype = SyncType.push;

            grid2.Visibility = System.Windows.Visibility.Visible;
            grid3.Visibility = System.Windows.Visibility.Hidden;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            progressBar1.Value = 0;
            button1.IsEnabled = true;

        }

        private void radioButton1_Checked(object sender, RoutedEventArgs e)
        {
            synctype = SyncType.pull;

            grid2.Visibility = System.Windows.Visibility.Visible;
            grid3.Visibility = System.Windows.Visibility.Hidden;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            progressBar1.Value = 0;
            button1.IsEnabled = true;
        }

        private void radioButton4_Checked(object sender, RoutedEventArgs e)
        {
            syncdetail = SyncDetail.def;

            grid3.Visibility = System.Windows.Visibility.Hidden;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            progressBar1.Value = 0;
            button1.IsEnabled = true;
        }

        private void radioButton5_Checked(object sender, RoutedEventArgs e)
        {
            syncdetail = SyncDetail.man;

            grid3.Visibility = System.Windows.Visibility.Visible;
            grid4.Visibility = System.Windows.Visibility.Hidden;

            progressBar1.Value = 0;
            button1.IsEnabled = false;

            if (synctype == SyncType.push)
            {
                comboBox1.ItemsSource = Enum.GetValues(typeof(push_table_opt));
            }
            else if (synctype == SyncType.pull)
                comboBox1.ItemsSource = Enum.GetValues(typeof(pull_table_opt));

        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.HasItems)
                button1.IsEnabled = false;
        }

        #endregion
    }
}
